﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;

using CE.User;
using CE.Global;
using CE.Database;
using CE.CarInsurance;
using CE.Mail;

public partial class car_insurance_complete_quote : MyLanguageBasePage
{
    string _redirectUrl = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] != null && Session[CE.Global.General.UserTypeId] != null)
        {
            if (Convert.ToInt32(Session[CE.Global.General.UserTypeId]) !=
                Convert.ToInt32(CE.Global.CEUserType.Customer) && Convert.ToInt32(Session[CE.Global.General.UserTypeId]) !=
                Convert.ToInt32(CE.Global.CEUserType.Provider))
            {
                Response.Redirect("default.aspx");
            }
        }
        else
        {
            Response.Redirect("default.aspx");
        }

        if (Session["ViewCIResultId"] == null)
        {
            Response.Redirect("~/user/saved-quotes.aspx");
        }

        if (!IsPostBack)
        {
            txtExEmail.Text = Session[CE.Global.General.UserEmail].ToString();

            if (Session["ViewCIResultId"] != null)      // || Page.Request.Params["ResultId"] != null)
            {
                Int64 _tempResultId = 0;
                Int64 _carId = 0;
                if (Int64.TryParse(Convert.ToString(Session["ViewCIResultId"]), out _tempResultId))       //(Int64.TryParse(Page.Request.Params["ResultId"], out _tempResultId))
                {
                    _carId = CheckCompleteStatus(_tempResultId);

                    if (_carId > 0)
                    {
                        ShowDetails(_carId);
                    }
                    else
                    {
                        Response.Redirect("~/user/saved-quotes.aspx");
                    }
                }
                else
                {
                    Response.Redirect("~/user/saved-quotes.aspx");
                }
            }
        }

    }

    private Int64 CheckCompleteStatus(Int64 _resultId)
    {
        UserResultHistory _result = new UserResultHistory();
        UserResultHistoryDB _resultdb = new UserResultHistoryDB();
        Int64 _carId = 0;
        Int64 CustomerId = 0;
        if (Session[CE.Global.General.UserId] != null)
        {
            CustomerId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());
        }
        _result = _resultdb.GetResultHistory(Convert.ToInt64(Session[General.UserId].ToString()), _resultId);
        if (_result != null && _result.IsComplete)
        {
            _carId = _result.ID1;
        }
        else
        {
            DataSet _ds = GetCarDetails(_result.ID1, CustomerId);
            if (_ds != null && _ds.Tables.Count > 2 && _ds.Tables[2].Rows.Count > 0)
            {
                _carId = _result.ID1;
            }
        }

        return _carId;
    }

    private void ShowDetails(Int64 _carId)
    {
        Int64 CustomerId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());
        hidGetStep.Value = "1";
        DataSet _dsOffline = new DataSet();
        _dsOffline = GetCarDetails(_carId, CustomerId);
        if (_dsOffline != null && _dsOffline.Tables.Count > 2 && _dsOffline.Tables[2].Rows.Count > 0)
        {
            hidGetStep.Value = "2";
            ShowNonGGDetails(_dsOffline);
            return;
        }
        Int64 _refCEUserId = Convert.ToInt64(Convert.ToInt64(Session[General.UserId].ToString()));
        string _refNo = "";
        CarDetailTH _cardetailth = new CarDetailTH();
        CarDetailTHDB _cardetailthdb = new CarDetailTHDB();
        CE.Database.MyDatabase _db = new CE.Database.MyDatabase();
        DataSet _dsInsurance = new DataSet();

        Int64 _companyId = 0;

        System.Data.DataTable _dtSeletectItem = new System.Data.DataTable("SelectedResultItem");
        _dtSeletectItem = _db.GetDataTable("Select * from tblCarInsuranceSelectItemTH WHERE CarId = " + _carId.ToString());
        if (_dtSeletectItem != null && _dtSeletectItem.Rows.Count > 0)
        {
            _companyId = Convert.ToInt64(_dtSeletectItem.Rows[0]["ProviderCompanyId"].ToString());
            lblAnnualPremium.Text = "$ " + Convert.ToSingle(_dtSeletectItem.Rows[0]["AnnualPremium"].ToString()).ToString("#.00");
        }
        CarInsuranceCompletedItemDB _carinsurancecompleteditemdb = new CarInsuranceCompletedItemDB();
        _dsInsurance = _carinsurancecompleteditemdb.GetCarInsuranceCompletedItem(_carId, _companyId);
        if (_dsInsurance != null && _dsInsurance.Tables[0].Rows.Count > 0)
        {
            lblGender.Text = _dsInsurance.Tables[0].Rows[0]["Gender"].ToString() == "Male" ? Resources.CarInsurance.Male : Resources.CarInsurance.Female;
            lblDOB.Text = Convert.ToDateTime(_dsInsurance.Tables[0].Rows[0]["DOB"]).ToString("dd-MMM-yyyy");
            if (CE.Global.General.SetCulture() == "th-TH")
            {
                lblOccupation.Text = _dsInsurance.Tables[0].Rows[0]["THOccupation"].ToString();
            }
            else
            {
                lblOccupation.Text = _dsInsurance.Tables[0].Rows[0]["ENOccupation"].ToString();
            }

            //lblInsuranceType.Text = _dsInsurance.Tables[0].Rows[0]["InsuranceType"].ToString();
            switch (_dsInsurance.Tables[0].Rows[0]["InsuranceType"].ToString())
            {
                case "Class 1":
                    lblInsuranceType.Text = Resources.CarInsurance.Insurance_ClassOne;
                    break;
                case "Class 1 (Low Cost)":
                    lblInsuranceType.Text = Resources.CarInsurance.Insurance_ClassOneLowCost;
                    break;
                case "Class 2+":
                    lblInsuranceType.Text = Resources.CarInsurance.Insurance_ClassTwoPlus;
                    break;
                case "Class 3+":
                    lblInsuranceType.Text = Resources.CarInsurance.Insurance_ClassThreePlus;
                    break;
                case "Class 3":
                    lblInsuranceType.Text = Resources.CarInsurance.Insurance_ClassThree;
                    break;
            }

            hplViewGarage.NavigateUrl = SetViewGarageUrl(_companyId.ToString());
            lblGarageType.Text = _dsInsurance.Tables[0].Rows[0]["GarageType"].ToString() == "Authorized" ? Resources.CarInsurance.Garage_Authorized : Resources.CarInsurance.Garage_Dealer;
            lblCarModelYear.Text = _dsInsurance.Tables[0].Rows[0]["CarModelYear"].ToString();
            lblCarMakeModel.Text = _dsInsurance.Tables[0].Rows[0]["MakeModelDesc"].ToString();
            lblVehicleType.Text = _dsInsurance.Tables[0].Rows[0]["VehicleType"].ToString() == "Private Use" ? Resources.CarInsurance.VehicleType_Private : Resources.CarInsurance.VehicleType_Commercial;
            lblRedPlate.Text = _dsInsurance.Tables[0].Rows[0]["RedPlate"].ToString();
            lblSumInsured.Text = Convert.ToSingle(_dsInsurance.Tables[0].Rows[0]["SumInsured"]).ToString("#,###") + " " + Resources.CarInsurance.Baht;
            lblProvinceofRegistration.Text = _dsInsurance.Tables[0].Rows[0]["ProvinceRegistration"].ToString() == "Bangkok Metro Area" ? Resources.CarInsurance.Province_BKK : Resources.CarInsurance.Province_UPC;
            lblAnyClaim.Text = _dsInsurance.Tables[0].Rows[0]["AnyClaim"].ToString() == "Yes" ? Resources.CarInsurance.Option_YesHave : Resources.CarInsurance.Option_NoHave;
            lblNCB.Text = _dsInsurance.Tables[0].Rows[0]["NCB"].ToString() + "%";
            lblAddDriver.Text = _dsInsurance.Tables[0].Rows[0]["Driver"].ToString() == "Yes" ? Resources.CarInsurance.Option_YesSpecify : Resources.CarInsurance.Option_NoSpecify;
            if (_dsInsurance.Tables[0].Rows[0]["CommenceDate"] != DBNull.Value)
            {
                lblCoverCommenceDate.Text = Convert.ToDateTime(_dsInsurance.Tables[0].Rows[0]["CommenceDate"]).ToString("dd-MMM-yyyy");
            }

            if (_dsInsurance.Tables.Count > 1)
            {
                if (_dsInsurance.Tables[1].Rows.Count > 0)
                {
                    object moddate = _db.ExecuteScalarSqlStatement("SELECT Modifieddate FROM tblCarPolicy WHERE CECarId = " + _carId.ToString());
                    if (moddate != null)
                        hidModDate.Value = Convert.ToDateTime(moddate).ToString("dd-MMM-yyyy", new System.Globalization.CultureInfo("en-US"));

                    if (_dsInsurance.Tables[1].Rows[0]["CoverTypeDesc"].ToString() != "GluayGluay.com Special")
                    {
                        lblInsurer.Text = GetInsurer(_companyId);
                        lblPackage.Text = _dsInsurance.Tables[1].Rows[0]["CoverTypeDesc"].ToString();
                    }
                    else
                    {
                        lblInsurance.Text = Resources.ProposalForm.Package;
                        lblInsurer.Text = _dsInsurance.Tables[1].Rows[0]["CoverTypeDesc"].ToString();
                        lblPackageName.Text = Resources.ProposalForm.PackageCondition;
                        lblPackage.Text = Resources.CarInsuranceResultPage.SpecialNCB;

                        hplViewGarage.Visible = false;
                    }
                    lblAnnualPremium.Text = Convert.ToSingle(_dsInsurance.Tables[1].Rows[0]["AnnualPremium"]).ToString("#,###.00") + " " + Resources.CarInsurance.Baht;
                    lblCTPL.Text = _dsInsurance.Tables[1].Rows[0]["CTPL"].ToString() == "Yes" ? Resources.CarInsurance.Option_Yes : Resources.CarInsurance.Option_No;
                    if (_dsInsurance.Tables[1].Rows[0]["CTPL"].ToString() != "Yes")
                        div4.Style.Add("display", "none");
                    lblCTPLAmount.Text = Convert.ToSingle(_dsInsurance.Tables[1].Rows[0]["CTPLAmount"]).ToString("#.00") + " " + Resources.CarInsurance.Baht;
                    

                    lblName.Text = _dsInsurance.Tables[1].Rows[0]["Name"].ToString();
                    lblIdentificationNo.Text = "";
                    if (_dsInsurance.Tables[1].Rows[0]["IdentificationNo"] != null)
                    {
                        lblIdentificationNo.Text = _dsInsurance.Tables[1].Rows[0]["IdentificationNo"].ToString();
                    }
                    lblContactNo.Text = String.Format("{0:0##-###-####}", Convert.ToInt32(_dsInsurance.Tables[1].Rows[0]["ContactNo"]));
                    lblVehicleNo.Text = _dsInsurance.Tables[1].Rows[0]["VehicleNo"].ToString();
                    lblEngineNo.Text = _dsInsurance.Tables[1].Rows[0]["EngineNo"].ToString();
                    lblChassisNo.Text = _dsInsurance.Tables[1].Rows[0]["ChassisNo"].ToString();
                    lblVehicleColor.Text = _dsInsurance.Tables[1].Rows[0]["VehicleColor"].ToString();
                    lblVehicleProvince.Text = _dsInsurance.Tables[1].Rows[0]["VehicleProvince"].ToString();
                    if (_dsInsurance.Tables[1].Rows[0]["PolicyStartDate"] != DBNull.Value)
                    {
                        lblPolicyStartDate.Text = Convert.ToDateTime(_dsInsurance.Tables[1].Rows[0]["PolicyStartDate"]).ToString("dd-MMM-yyyy");
                        lblPolicyEndDate.Text = Convert.ToDateTime(_dsInsurance.Tables[1].Rows[0]["PolicyStartDate"]).AddYears(1).ToString("dd-MMM-yyyy");
                    }
                    _refNo = _dsInsurance.Tables[1].Rows[0]["QuoteRefNo"].ToString();

                    //if (_dsInsurance.Tables[3].Rows.Count > 0)
                    //{
                    //    divFirstDriver.Style.Add("display", "");
                    //    lblDriverFirst.Text = _dsInsurance.Tables[3].Rows[0]["FullName"].ToString();
                    //}
                    //if (_dsInsurance.Tables[3].Rows.Count > 1)
                    //{
                    //    divSecondDriver.Style.Add("display", "");
                    //    lblDriverTwo.Text = _dsInsurance.Tables[3].Rows[1]["FullName"].ToString();
                    //}

                    //Sakthi on 12 Jan 2012  -- Start
                    switch (_dsInsurance.Tables[1].Rows[0]["PaymentType"].ToString())
                    {
                        case "CreditCard":
                            lblPaymentType.Text = Resources.ProposalForm.CreditCard;
                            break;
                        case "Bank In":
                            lblPaymentType.Text = Resources.ProposalForm.BankIn;
                            break;
                    }
                    switch (_dsInsurance.Tables[1].Rows[0]["PaymentStatus"].ToString())
                    {
                        case "True":
                            lblPaymentStatus.Text = Resources.ProposalForm.Yes;
                            break;
                        case "False":
                            lblPaymentStatus.Text = Resources.ProposalForm.No;
                            break;
                    }
                    if (_dsInsurance.Tables[1].Rows[0]["PaymentType"].ToString() == "CreditCard")
                    {
                        switch (_dsInsurance.Tables[1].Rows[0]["PaymentCode"].ToString())
                        {
                            case "0":
                                lblPaymentStatus.Text += " - " + Resources.ProposalForm.Fail;
                                break;
                            case "1":
                                lblPaymentStatus.Text += " - " + Resources.ProposalForm.Success;
                                break;
                            case "2":
                                lblPaymentStatus.Text += " - " + Resources.ProposalForm.Cancel;
                                break;
                        }
                    }
                    //Sakthi on 12 Jan 2012  -- End
                }
            }

            lblQuoteTitle.Text = Resources.CarInsurance.QuoteRefNo + " : " + _refNo + " - " + Resources.CarInsurance.CarInsuranceList + " " + lblCarMakeModel.Text;
            hidRefNo.Value = _refNo;
        }
    }

    private void ShowNonGGDetails(DataSet DsNonGG)
    {
        if (DsNonGG != null && DsNonGG.Tables.Count > 2 && DsNonGG.Tables[2].Rows.Count > 0)
        {
            Int64 _refCEUserId = Convert.ToInt64(Convert.ToInt64(Session[General.UserId].ToString()));
            string _refNo = "";
            Int64 _companyId = Convert.ToInt64(DsNonGG.Tables[2].Rows[0]["ProviderCompanyId"].ToString());
            lblAnnualPremium.Text = "$ " + Convert.ToSingle(DsNonGG.Tables[2].Rows[0]["TotalCollectedPremium"].ToString()).ToString("#.00");

            lblGender.Text = DsNonGG.Tables[2].Rows[0]["Gender"].ToString() == "M" ? Resources.CarInsurance.Male : Resources.CarInsurance.Female;
            lblDOB.Text = Convert.ToDateTime(DsNonGG.Tables[0].Rows[0]["DOB"]).ToString("dd-MMM-yyyy");
            if (CE.Global.General.SetCulture() == "th-TH")
            {
                lblOccupation.Text = DsNonGG.Tables[2].Rows[0]["OccupationTH"].ToString();
            }
            else
            {
                lblOccupation.Text = DsNonGG.Tables[2].Rows[0]["OccupationEN"].ToString();
            }

            switch (DsNonGG.Tables[2].Rows[0]["InsuranceType"].ToString())
            {
                case "10":
                    lblInsuranceType.Text = Resources.CarInsurance.Insurance_ClassOne;
                    break;
                case "11":
                    lblInsuranceType.Text = Resources.CarInsurance.Insurance_ClassOneLowCost;
                    break;
                case "21":
                    lblInsuranceType.Text = Resources.CarInsurance.Insurance_ClassTwoPlus;
                    break;
                case "31":
                    lblInsuranceType.Text = Resources.CarInsurance.Insurance_ClassThreePlus;
                    break;
                case "30":
                    lblInsuranceType.Text = Resources.CarInsurance.Insurance_ClassThree;
                    break;
            }

            hplViewGarage.NavigateUrl = SetViewGarageUrl(_companyId.ToString());
            lblGarageType.Text = DsNonGG.Tables[2].Rows[0]["GarageType"].ToString() == "1" ? Resources.CarInsurance.Garage_Authorized : Resources.CarInsurance.Garage_Dealer;
            lblCarModelYear.Text = DsNonGG.Tables[2].Rows[0]["CarYear"].ToString();
            lblCarMakeModel.Text = DsNonGG.Tables[2].Rows[0]["CarModel"].ToString();
            lblVehicleType.Text = DsNonGG.Tables[2].Rows[0]["VehicleType"].ToString() == "110" ? Resources.CarInsurance.VehicleType_Private : Resources.CarInsurance.VehicleType_Commercial;
            lblRedPlate.Text = DsNonGG.Tables[2].Rows[0]["CarYear"].ToString();
            lblSumInsured.Text = Convert.ToSingle(DsNonGG.Tables[0].Rows[0]["SumInsured"]).ToString("#,###") + " " + Resources.CarInsurance.Baht;
            lblProvinceofRegistration.Text = DsNonGG.Tables[2].Rows[0]["CarPlateProvince"].ToString();
            lblAnyClaim.Text = Resources.CarInsurance.Option_NoHave;
            if (DsNonGG.Tables[2].Rows[0]["IsClaim"] != DBNull.Value && DsNonGG.Tables[2].Rows[0]["IsClaim"].ToString() != string.Empty)
            {
                lblAnyClaim.Text = DsNonGG.Tables[2].Rows[0]["IsClaim"].ToString() == "1" ? Resources.CarInsurance.Option_YesHave : Resources.CarInsurance.Option_NoHave;
            }
            lblNCB.Text = DsNonGG.Tables[2].Rows[0]["NCB"].ToString() + "%";
            lblAddDriver.Text = DsNonGG.Tables[2].Rows[0]["NoOfDriver"].ToString() != "0" ? Resources.CarInsurance.Option_YesSpecify : Resources.CarInsurance.Option_NoSpecify;
            lblCoverCommenceDate.Text = Convert.ToDateTime(DsNonGG.Tables[2].Rows[0]["CoverageStart"]).ToString("dd-MMM-yyyy");

            lblInsurer.Text = GetInsurer(Int32.Parse(DsNonGG.Tables[2].Rows[0]["ProviderCompanyId"].ToString())); //DsNonGG.Tables[2].Rows[0]["CurrentInsurer"].ToString();
            lblAnnualPremium.Text = Convert.ToSingle(DsNonGG.Tables[2].Rows[0]["TotalCollectedPremium"]).ToString("#,###.00") + " " + Resources.CarInsurance.Baht;
            lblCTPL.Text = DsNonGG.Tables[2].Rows[0]["IsCTPL"].ToString() == "True" ? Resources.CarInsurance.Option_Yes : Resources.CarInsurance.Option_No;
            if (DsNonGG.Tables[2].Rows[0]["IsCTPL"].ToString() != "True")
                div4.Style.Add("display", "none");    
            lblCTPLAmount.Text = Convert.ToSingle(DsNonGG.Tables[2].Rows[0]["CTPL"]).ToString("#.00") + " " + Resources.CarInsurance.Baht;
            lblPackage.Text = DsNonGG.Tables[2].Rows[0]["Package"].ToString();

            lblName.Text = DsNonGG.Tables[2].Rows[0]["Name"].ToString();
            lblIdentificationNo.Text = "N/A";

            lblContactNo.Text = String.Format("{0:0##-###-####}", Convert.ToInt32(DsNonGG.Tables[2].Rows[0]["ContactNo"]));
            lblVehicleNo.Text = DsNonGG.Tables[2].Rows[0]["CarPlate"].ToString();
            lblEngineNo.Text = DsNonGG.Tables[2].Rows[0]["EngineNumber"].ToString();
            lblChassisNo.Text = DsNonGG.Tables[2].Rows[0]["ChassisNumber"].ToString();
            lblVehicleProvince.Text = DsNonGG.Tables[2].Rows[0]["CarPlateProvince"].ToString();
            lblPolicyStartDate.Text = Convert.ToDateTime(DsNonGG.Tables[2].Rows[0]["CoverageStart"]).ToString("dd-MMM-yyyy"); //CTPLStart
            lblPolicyEndDate.Text = Convert.ToDateTime(DsNonGG.Tables[2].Rows[0]["CoverageStart"]).AddYears(1).ToString("dd-MMM-yyyy");
            hidModDate.Value = Convert.ToDateTime(DsNonGG.Tables[2].Rows[0]["Modifieddate"]).ToString("dd-MMM-yyyy", new System.Globalization.CultureInfo("en-US"));
            _refNo = DsNonGG.Tables[2].Rows[0]["QuoteRefNo"].ToString();

            lblPolicyNumber.Text = GetPolicyNumber(Int32.Parse(DsNonGG.Tables[2].Rows[0]["RefCEUserId"].ToString()),
                    Int32.Parse(DsNonGG.Tables[2].Rows[0]["CECarId"].ToString()),
                    DsNonGG.Tables[2].Rows[0]["QuoteRefNo"].ToString());

            if (DsNonGG.Tables[3].Rows.Count > 0)
            {
                divFirstDriver.Style.Add("display", "");
                lblDriverFirst.Text = DsNonGG.Tables[3].Rows[0]["FullName"].ToString();
            }
            if (DsNonGG.Tables[3].Rows.Count > 1)
            {
                divSecondDriver.Style.Add("display", "");
                lblDriverTwo.Text = DsNonGG.Tables[3].Rows[1]["FullName"].ToString();
            }

            lblPaymentType.Text = "N/A";
            switch (DsNonGG.Tables[2].Rows[0]["PaymentStatus"].ToString())
            {
                case "1":
                    lblPaymentStatus.Text = Resources.ProposalForm.Yes;
                    break;
                case "0":
                    lblPaymentStatus.Text = Resources.ProposalForm.No;
                    break;
            }
            lblQuoteTitle.Text = Resources.CarInsurance.QuoteRefNo + " : " + _refNo + " - " + Resources.CarInsurance.CarInsuranceList + " " + lblCarMakeModel.Text;
            hidRefNo.Value = _refNo;
        }
    }

    //protected void ibtnBack_Click(object sender, ImageClickEventArgs e)
    //{
    //    Response.Redirect("~/user/viewresulthistory.aspx");
    //}

    protected void btnBack_Click(object sender, EventArgs e)
    {
        _redirectUrl = CE.Global.General.GetUrl("~/user/saved-quotes.aspx", Page);
        Response.Redirect(_redirectUrl);
    }

    public string GetInsurer(Int64 CompanyId)
    {
        MyDatabase _db = new MyDatabase();
        object CompanyName;
        if (CE.Global.General.SetCulture() == "th-TH")
        {
            CompanyName = _db.ExecuteScalarSqlStatement("SELECT THCompanyNameDescription FROM tblProviderCompany WHERE ProviderCompanyId = " + CompanyId.ToString());
        }
        else
        {
            CompanyName = _db.ExecuteScalarSqlStatement("SELECT CompanyNameDescription FROM tblProviderCompany WHERE ProviderCompanyId = " + CompanyId.ToString());
        }

        string _Insurer = "";
        if (CompanyName != null)
        {
            _Insurer = CompanyName.ToString();
        }

        //if (CompanyId == 11)
        //{
        //    _Insurer = Resources.Provider.AZCP; // "Allianz Insurance";
        //}
        //else if (CompanyId == 12)
        //{
        //    _Insurer = Resources.Provider.MSIG;  //"MSIG Insurance";
        //}
        //else if (CompanyId == 16)
        //{
        //    _Insurer = Resources.Provider.AXA;  //"AXA Insurance";
        //}
        //else if (CompanyId == 29)
        //{
        //    _Insurer = Resources.Provider.LMG; //"LMG Insurance";
        //}
        //else if (CompanyId == 31)
        //{
        //    _Insurer = Resources.Provider.Chartis; //"Chartis Insurance";
        //}
        //else if (CompanyId == 32)
        //{
        //    _Insurer = Resources.Provider.BKI; //"Bangkok Insurance";
        //}
        //else if (CompanyId == 33)
        //{
        //    _Insurer = Resources.Provider.Muangthai; //"Muang Thai Insurance";
        //}
        //else if (CompanyId == 41)
        //{
        //    _Insurer = Resources.Provider.Kurnia; //"Kurnia Insurance";
        //}
        //else if (CompanyId == 46)
        //{
        //    _Insurer = Resources.Provider.Tokio; //"Tokio Insurance";
        //}
        //else if (CompanyId == 65)
        //{
        //    _Insurer = Resources.Provider.Dhipaya; //"Dhipaya Insurance";
        //}
        //else
        //{
        //    _Insurer = Resources.CarInsurance.Others;
        //}
        return _Insurer;
    }

    public string GetPolicyNumber(Int32 RefCEUserId,Int32 CECarId, string QuoteRefNo)
    {
        MyDatabase _db = new MyDatabase();
        object PolicyNumber;
        PolicyNumber = _db.ExecuteScalarSqlStatement("SELECT Remarks FROM tblNonGGTransaction WHERE TransactionTypeID = 400 AND RefCEUserId = " + RefCEUserId.ToString() + " AND CECarId = " + CECarId.ToString() + " AND QuoteRefNo = '" + QuoteRefNo + "' Order By ModifiedDate DESC");

        string _PolicyNumber = "";
        if (PolicyNumber != null)
        {
            _PolicyNumber = PolicyNumber.ToString();
        }

        return _PolicyNumber;
    }

    private string SetViewGarageUrl(string _companyId)
    {
        string url = string.Empty;
        if (_companyId == "11")
        {
            // "Allianz C.P";
            if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
            {
                url = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.AllianceWorkshopTH");
            }
            else
            {
                url = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.AllianceWorkshopEN");
            }

        }
        else if (_companyId == "12")
        {
            //lblPromo.Text = "MSIG";
            if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
            {
                url = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.MSIGWorkshopTH");
            }
            else
            {
                url = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.MSIGWorkshopEN");
            }
        }
        else if (_companyId == "16")
        {
            //lblPromo.Text = "AXA";
            if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
            {
                url = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.AXAWorkshopTH");
            }
            else
            {
                url = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.AXAWorkshopEN");
            }
        }
        else if (_companyId == "29")
        {
            //lblPromo.Text = "LMG Insurance";
            if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
            {
                url = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.LMGWorkshopTH");
            }
            else
            {
                url = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.LMGWorkshopEN");
            }
        }
        else if (_companyId == "31")
        {
            //lblPromo.Text = "Chartis";
            if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
            {
                url = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.ChartisWorkshopTH");
            }
            else
            {
                url = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.ChartisWorkshopEN");
            }
        }
        else if (_companyId == "32")
        {
            //lblPromo.Text = "Bangkok Insurance";
            if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
            {
                url = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.BKIWorkshopTH");
            }
            else
            {
                url = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.BKIWorkshopEN");
            }
        }
        else if (_companyId == "33")
        {
            //lblPromo.Text = "MTI Insurance";
            if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
            {
                url = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.MTIWorkshopTH");
            }
            else
            {
                url = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.MTIWorkshopTH");
            }
        }
        else if (_companyId == "35")
        {

            if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
            {
                url = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.KPIWorkshopTH");
            }
            else
            {
                url = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.KPIWorkshopTH");
            }
        }
        else if (_companyId == "39")
        {

            if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
            {
                url = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.MittareWorkshopTH");
            }
            else
            {
                url = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.MittareWorkshopEN");
            }
        }
        else if (_companyId == "41")
        {
            //lblPromo.Text = KSK "Kurnia Insurance";  
            if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
            {
                url = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.KSKWorkShopTH");
            }
            else
            {
                url = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.KSKWorkShopTH");
            }
        }
        else if (_companyId == "43")
        {
            //lblPromo.Text = "Syn Mun Kong Insurance";
            if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
            {
                url = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.SMKWorkShopTH");
            }
            else
            {
                url = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.SMKWorkShopTH");
            }
        }
        else if (_companyId == "45")
        {
            //lblPromo.Text = "Viriyah Insurance";
            if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
            {
                url = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.ViriyahWorkShopTH");
            }
            else
            {
                url = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.ViriyahWorkShopTH");
            }
        }
        else if (_companyId == "46")
        {
            //lblPromo.Text = "TMS Insurance";
            if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
            {
                url = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.TokioWorkShopTH");
            }
            else
            {
                url = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.TokioWorkShopEH");
            }
        }
        else if (_companyId == "50")
        {
            //lblPromo.Text = "TMS Insurance";
            if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
            {
                url = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.SCSMGWorkshopTH");
            }
            else
            {
                url = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.SCSMGWorkshopEN");
            }
        }
        else if (_companyId == "58")
        {
            //lblPromo.Text = "TMS Insurance";
            if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
            {
                url = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.ThaivivatWorkShopTH");
            }
            else
            {
                url = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.ThaivivatWorkShopEN");
            }
        }
        else if (_companyId == "65")
        {
            //lblPromo.Text = "Dhipaya Insurance";
            if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
            {
                url = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.DhipayaWorkShopTH");
            }
            else
            {
                url = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.DhipayaWorkShopEN");
            }
        }
        return url;
    }

    public DataSet GetCarDetails(Int64 CarId, Int64 CustomerId)
    {
        MyDatabase _db = new MyDatabase();
        string _spName = "[sprocCXCRMNONGGPackageCarDetailsSelectedItemByCarId]";

        SortedList _spara = new SortedList();
        _spara.Add("@CarId", CarId);
        _spara.Add("@CustomerId", CustomerId);

        DataSet _ds = _db.GetDataSet(_spName, _spara);

        return _ds;
    }

    #region SendEmail

    protected void imgbtnEmail_Click(object sender, EventArgs e)
    {
        if (txtExEmail.Text.Trim() != "")
        {
            try
            {
                CompletedQuotePDFOutput _completedQuotePdf = CreateCompletedQuotePDFObject();
                string _fileName = _completedQuotePdf.GeneratePDF(hidGetStep.Value);
                if (_fileName != string.Empty)
                {
                    FileInfo file = new System.IO.FileInfo(_fileName);

                    if (file.Exists)
                    {
                        string[] _file = new string[1];
                        _file[0] = _fileName;

                        string _toemail = txtExEmail.Text.Trim().ToLower();
                        string _ccemail = string.Empty;//Session[General.UserEmail].ToString();

                        string _bccemail = string.Empty;//CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.ADMIN");
                        string _customerName = Session[CE.Global.General.UserName].ToString();
                        //string imgUrl = "http://" + HttpContext.Current.Request.Url.Host.ToString() + CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.LogoPath");
                        string _emailTitle = string.Empty;
                        string mailBody = "";

                        if (CE.Global.General.SetCulture() == "th-TH")
                        {
                            if (lblVehicleNo.Text.Trim().Length > 2)
                            {
                                _emailTitle = "รายละเอียดประกันรถยนต์ ทะเบียน " + lblVehicleNo.Text + " - GluayGluay.com ";
                            }
                            else
                            {
                                _emailTitle = "รายละเอียดประกันรถยนต์ เลขที่อ้างอิง " + hidRefNo.Value.Trim() + " - GluayGluay.com ";
                            }

                            //_emailTitle = "GluayGluay.com: รายละเอียดประกันรถยนต์ คุณ " + Name + " " + VehicleNo;
                            mailBody = @"เรียน คุณ " + _customerName +
                            "<br/><br/><a href='http://www.gluaygluay.com'>GluayGluay.com</a> ขอส่งรายละเอียดประกันรถยนต์ของคุณ  โปรดดูที่ไฟล์แนบ <br/> " +
                            "หากท่านต้องการสอบถามรายละเอียดเพิ่มเติม กรุณาติดต่อ <strong>GluayGluay.com</strong> ได้ที่หมายเลขโทรศัพท์ <strong>02-268-8222</strong><br/> วันจันทร์ ถึง วันศุกร์ เวลา 9.00 – 18.00 หรือส่งอีเมล์มาหาเราที่  <strong>support@gluaygluay.com.</strong>";

                            mailBody += "<br/><br/>ขอแสดงความนับถือ<br/>" +
                            "<br/>ทีมงาน GluayGluay.com<br/><br/>" +
                                //Company Name changed
                            "บริษัท คอมแพร์เอ็กซ์เพรส นายหน้าประกันภัย (ประเทศไทย) จำกัด<br/>" +
                            "เลขทะเบียนนายหน้าประกันวินาศภัยนิติบุคคล เลขที่ ว00016/2553<br/>" +
                            "2 อาคาร เค.ซี.ซี. ชั้น 2 สีสมซอย 9 ถนนสีลม เขตบางรัก 10500<br/>" +
                            "เว็บไซต์: <a href='http://www.GluayGluay.com'>www.GluayGluay.com</a> | โทร.: 02-268 8222 | อีเมล์: support@gluaygluay.com<br/>" +
                            "Follow us: <a href='http://www.facebook.com/GGmotor'>www.facebook.com/GGmotor</a> | Instagram: #GGmotor<br/>" +
                            "<br/><br/>" +
                            "<a href='http://www.gluaygluay.com'><img src='http://www.gluaygluay.com/images/public/logo/gluaygluay-plakat_export.jpg' alt='GluayGluay.com'/></a>";
                        }
                        else
                        {
                            //_emailTitle = "Completed Quote from gluaygluay.com for " + lblName.Text + " " + lblVehicleNo.Text;
                            if (lblVehicleNo.Text.Trim().Length > 2)
                            {
                                _emailTitle = "Completed Car Details [" + lblVehicleNo.Text + "] on GluayGluay.com ";
                            }
                            else
                            {
                                _emailTitle = "Completed Car Details [" + hidRefNo.Value.Trim() + "] on GluayGluay.com ";
                            }
                            mailBody = @"Dear " + _customerName +
                            "<br/><br/>Please find attached in PDF a copy of your completed car insurance details on <a href='http://www.gluaygluay.com'>GluayGluay.com.</a> " +
                            "<br/><br/>We are ready to assist at <strong>02-268-8222</strong> Monday to Fridays from 9am to 6pm. <br/>Alternatively, you can email us at <strong>support@gluaygluay.com</strong>.";

                            mailBody += "<br/><br/>Best regards,<br/>" +
                            "<br/>Support GluayGluay.com<br/><br/>" +
                            "Comparexpress Insurance Broker ( Thailand ) Ltd,<br/>" +
                            "OIC Licensed Non-Life Insurance Broker: ว00016/2553<br/>" +
                            "2 K.C.C Building 2nd floor, Silom Soi9<br/>" +
                            "Silom Road, Bangrak, Bangkok 10500<br/>" +
                            "W: <a href='http://www.GluayGluay.com'>www.GluayGluay.com</a> | T: 02-268 8222 | E: support@gluaygluay.com<br/>" +
                            "Follow us: <a href='http://www.facebook.com/GGmotor'>www.facebook.com/GGmotor</a> | Instagram: #GGmotor<br/>" +
                            "<br/><br/>" +
                            "<a href='http://www.gluaygluay.com'><img src='http://www.gluaygluay.com/images/public/logo/gluaygluay-plakat_export.jpg' alt='GluayGluay.com'/></a>";
                        }

                        bool _mailSendingStatus = false;

                        _mailSendingStatus = MailManagerTH.SendMailWithAttach(_customerName, _toemail, _ccemail, _bccemail, _emailTitle, mailBody, _file);

                        if (_mailSendingStatus)
                        {
                            mpeEmail.Hide();
                        }
                        else
                        {
                            lblEmailMessage.Text = Resources.ErrorMessage.EmailFail; //"Email sending failed.";
                        }
                    }
                    else
                    {
                        lblEmailMessage.Text = Resources.ErrorMessage.PDFFail; //"PDF generating failed.";
                    }
                }
                else
                {
                    lblEmailMessage.Text = Resources.ErrorMessage.PDFNoItem; //"PDF generating failed. No item found.";
                }

            }
            catch (Exception Ex)
            {

                lblEmailMessage.Text = Ex.Message;
            }
        }
    }

    private CompletedQuotePDFOutput CreateCompletedQuotePDFObject()
    {
        CompletedQuotePDFOutput _nonGGPdf = new CompletedQuotePDFOutput();
        //_nonGGPdf.Title = "GluayGluay.com - " + Resources.Menu.CarInsurance;
        _nonGGPdf.Title = Resources.CarInsurance.PDF_Title; // "รายละเอียดประกันรถยนต์";

        _nonGGPdf.QuoteRefTitle = lblQuoteTitle.Text;
        _nonGGPdf.CustomerName = lblName.Text;
        _nonGGPdf.CarPlateNumber = lblVehicleNo.Text;
        _nonGGPdf.Email = txtExEmail.Text;
        _nonGGPdf.DOB = lblDOB.Text;
        _nonGGPdf.Mobile = lblContactNo.Text;
        _nonGGPdf.Gender = lblGender.Text;
        _nonGGPdf.Occupation = lblOccupation.Text;

        _nonGGPdf.CurrentInsurer = lblInsurer.Text;
        _nonGGPdf.CarMakeModel = lblCarMakeModel.Text;
        _nonGGPdf.CarModelYear = lblCarModelYear.Text;
        _nonGGPdf.AnyClaim = lblAnyClaim.Text;
        _nonGGPdf.CommnecementDate = lblPolicyStartDate.Text;
        _nonGGPdf.CommnecementEndDate = lblPolicyEndDate.Text;
        _nonGGPdf.ModifyDate = hidModDate.Value;
    
        _nonGGPdf.SumInsuredAmount = lblSumInsured.Text;
        _nonGGPdf.InsuranceType = lblInsuranceType.Text;
        _nonGGPdf.GarageType = lblGarageType.Text;
        
        _nonGGPdf.VehicleTypeUsage = lblVehicleType.Text;
        _nonGGPdf.IncludeCTPL = lblCTPL.Text;
        _nonGGPdf.NCB = lblNCB.Text;
        _nonGGPdf.NamedDriver = lblAddDriver.Text;
        _nonGGPdf.Driver1 = lblDriverFirst.Text;
        _nonGGPdf.Driver2 = lblDriverTwo.Text;
        _nonGGPdf.PolicyNumber = lblPolicyNumber.Text;
        _nonGGPdf.Package = lblPackage.Text;
        _nonGGPdf.Premium = lblAnnualPremium.Text;
        _nonGGPdf.CTPLAmount = lblCTPLAmount.Text;
        _nonGGPdf.IdentificationNo = lblIdentificationNo.Text;

        return _nonGGPdf;
    }

    #endregion

}
